<template>
  <div class="staff">
    <!--S头部-->
    <div id="header">
      <el-alert
        title="员工可以收到用户素材和卡券审核通知"
        type="success"
        :closable="false"
        style="width: 500px"
      >
      </el-alert>
      <el-button class="add-button" type="primary" size="small" @click="addStaff">添加员工</el-button>
    </div>
    <!--S头部-->

    <!--S员工列表-->
    <el-table :data="data">
      <el-table-column prop="member.nickname" label="昵称" />
      <el-table-column prop="member.avatar" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.member.avatar" style="width: 40px;height: 40px;" />
        </template>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="edit(scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--E员工列表-->

    <!--S添加员工区域-->
    <el-dialog
      :title="EditDialog.id ? '编辑员工' : '添加员工'"
      :visible.sync="EditDialog.show"
      @close="closeEditDialog"
    >
      <el-form
        ref="form"
        :model="EditDialog.form_data"
        :rules="EditDialog.form_data_rules"
      >
        <el-form-item label="用户信息" prop="member_id">
          <member-input
            v-model="EditDialog.form_data.member_id"
            :toggle="EditDialog.toggle">
          </member-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="EditDialog.form_data.email">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="EditDialog.show = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </span>
    </el-dialog>
    <!--E添加员工区域-->
  </div>
</template>

<script>
import MemberInput from '../../../components/MemberInput'
export default {
  name: 'Staff',
  components: {
    MemberInput
  },
  data() {
    return {
      data: [],
      EditDialog: {
        show: false, // 显示状态
        id: 0, // 操作id,
        toggle: 0, // 清空添加员工扫码定时器
        form_data: {
          member_id: 0,
          email: ''
        },
        form_data_rules: {
          member_id: [
            { required: true, message: '请设置用户信息', trigger: 'blur' },
            { type: 'number', min: 1, message: '请设置用户信息', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { type: 'email', message: '请输入正确的邮箱', trigger: 'blur' }
          ]
        }
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 添加员工
    addStaff() {
      this.EditDialog.form_data = {
        member_id: 0,
        email: ''
      }
      this.EditDialog.show = true
      this.EditDialog.id = 0
      this.EditDialog.toggle = new Date().getTime()
    },
    // 关闭添加修改员工区域
    closeEditDialog() {
      this.EditDialog.toggle = -1
    },
    // 获取员工列表
    getList() {
      this.$get('manage')
        .then((res) => {
          this.data = res.data.data
        })
    },
    // 提交
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.EditDialog.id) {
            this.$post('manage/' + this.EditDialog.id, {
              member_id: this.EditDialog.form_data.member_id,
              email: this.EditDialog.form_data.email
            })
              .then(() => {
                this.getList()
                this.EditDialog.show = false
              })
              .catch((error) => {
                this.$handleError(error)
              })
          } else {
            this.$post('manage', {
              member_id: this.EditDialog.form_data.member_id,
              email: this.EditDialog.form_data.email
            })
              .then(() => {
                this.getList()
                this.EditDialog.show = false
              })
              .catch((error) => {
                this.$handleError(error)
              })
          }
        }
      })
    },
    // 删除
    remove(manage_id) {
      this.$del('manage/' + manage_id)
        .then(() => {
          this.getList()
        })
        .catch((error) => {
          this.$handleError(error)
        })
    },
    // 修改
    edit(manage) {
      this.EditDialog.form_data = {
        member_id: manage.member_id,
        email: manage.email
      }
      this.EditDialog.show = true
      this.EditDialog.id = manage.id
      this.EditDialog.toggle = new Date().getTime()
    }
  }
}
</script>

<style scoped lang="scss">
  .staff{
    #header{
      position: relative;
      .add-button{
        position: absolute;
        right: 0;
        top:0;
      }
    }
  }
</style>
